/**
 * 
 */
var pxml = "";

var initpxml = function(pDviID) {
	var $pDviID = $("#" + pDviID);
	pxml = "<pxml format=\"pdf\" id=\"" + pDviID + "\" left=\"" + $pDviID.offset().left + "\" top=\"" + $pDviID.offset().top
			+ "\" width=\"" + $pDviID.width() + "\">";

};

var getDivInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"div\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" classes=\"" + $element1.attr("class") + "\" fontFamily=\""
						+ $element1.css("font-family") + "\" fontSize=\"" + $element1.css("font-size") + "\" fontStyle=\""
						+ $element1.css("font-style") + "\" fontWeight=\"" + $element1.css("font-weight") + "\" color=\""
						+ $element1.css("color") + "\" textAlign=\"" + $element1.css("text-align") + "\" disabled=\""
						+ $element1.attr("disabled") + "\" display=\"" + $element1.css("display") + "\" backgroundImage=\""
						+ getImgValue($element1.css("background-image")) + "\" backgroundColor=\"" + $element1.css("background-color")
						+ "\" borderBottom=\"" + $element1.css("border-bottom") + "\" margin=\"" + $element1.css("margin")
						+ "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getTextInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"text\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.val()) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getRadioInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"radio\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.attr("checked")) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getCheckBoxInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"checkBox\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.attr("checked")) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getLabelInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"label\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.text()) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getLabelExInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				var width = $element1.width() + $element1.next().width();
				pxml += "<node nodeType=\"label\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + width + "\" height=\""
						+ $element1.height() + "\" value=\"" + getStrValue($element1.parent().text()) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getLabel_htmlInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				var val = getStrValue($element1.html());
				pxml += "<node nodeType=\"label\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + val + "\" classes=\"" + $element1.attr("class")
						+ "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\"" + $element1.css("font-size")
						+ "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\"" + $element1.css("font-weight")
						+ "\" color=\"" + $element1.css("color") + "\" textAlign=\"" + $element1.css("text-align") + "\" disabled=\""
						+ $element1.attr("disabled") + "\" display=\"" + $element1.css("display") + "\" textDecoration=\""
						+ $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getliLabelInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"liLabel\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.text()) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" />";
			});
	return pxml;
};

var getButtonInfo = function(element, index, findname) {
	var pxml = "";
	$(findname, document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				pxml += "<node nodeType=\"button\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
						+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
						+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.text()) + "\" classes=\""
						+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
						+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
						+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
						+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
						+ $element1.css("display") + "\" backgroundImage=\"" + getImgValue($element1.css("background-image"));
				if ($element1.find("span").length > 0) {
					pxml += "\" textDecoration=\"" + $element1.find("span").css("text-decoration");
				} else {
					pxml += "\" textDecoration=\"" + $element1.css("text-decoration");
				}
				pxml += "\" />";
			});
	return pxml;
};

var objs2pxml = function(index, element) {
	pxml += "<page index=\"" + index + "\" elementID=\"" + element.id + "\" height=\"" + $(element).height() + "\">";
	pxml += getDivInfo(element, index, "div.sectionHeader");
	pxml += getTextInfo(element, index, ":text");
	pxml += getRadioInfo(element, index, ":radio");
	pxml += getTextInfo(element, index, "textarea");
	pxml += getLabelInfo(element, index, "h2:not(:has(label))");
	pxml += getLabelInfo(element, index, "h3:not(:has(label))");
	pxml += getLabelInfo(element, index, "a");
	pxml += getLabelInfo(element, index, "label");
	pxml += getLabelInfo(element, index, ".label");
	pxml += getLabelInfo(element, index, "#appointmentDate");
	pxml += getLabelExInfo(element, index, "#textareaWordCount_remarks");
	pxml += getLabelExInfo(element, index, "#textareaWordCount_details");
	pxml += getliLabelInfo(element, index, "li:has(label#pimRecordStatus)");
	pxml += getButtonInfo(element, index, ".buttonInner");
	pxml += getButtonInfo(element, index, "button");
	pxml += getCheckBoxInfo(element, index, ":checkbox");
	pxml += getLabel_htmlInfo(element, index, "#Lbl_Res_Address_English");
	pxml += getLabel_htmlInfo(element, index, "#Lbl_Res_Address_Chinese");
	pxml += getLabel_htmlInfo(element, index, "#Lbl_Cor_Address_English");
	pxml += getLabel_htmlInfo(element, index, "#Lbl_Cor_Address_Chinese");

	$(".xgrid", document.getElementById(element.id).contentWindow.document.body).each(
			function(index1, element1) {
				var $element1 = $(element1);
				if ("" != $element1.html()) {
					// $("img",$element1).remove();
					pxml += "<node nodeType=\"xgrid\" index=\"" + index + "_" + index1 + "\" id=\"" + element1.id + "\" left=\""
							+ $element1.offset().left + "\" top=\"" + $element1.offset().top + "\" width=\"" + $element1.width()
							+ "\" height=\"" + $element1.height() + "\" value=\"" + getStrValue($element1.text()) + "\" classes=\""
							+ $element1.attr("class") + "\" fontFamily=\"" + $element1.css("font-family") + "\" fontSize=\""
							+ $element1.css("font-size") + "\" fontStyle=\"" + $element1.css("font-style") + "\" fontWeight=\""
							+ $element1.css("font-weight") + "\" color=\"" + $element1.css("color") + "\" textAlign=\""
							+ $element1.css("text-align") + "\" disabled=\"" + $element1.attr("disabled") + "\" display=\""
							+ $element1.css("display") + "\" textDecoration=\"" + $element1.css("text-decoration") + "\" >";
					var $xhdr = $(".xhdr", $element1);
					var tablehd = getTablexml($xhdr);
					var $xhdr = $(".objbox", $element1);
					var tablebd = getTablexml($xhdr);
					var $pagingArea = $(".pagingArea", $element1);
					var pagingArea = "<pagingArea width=\"" + $pagingArea.width() + "\" height=\"" + $pagingArea.height() + "\" color=\""
							+ $pagingArea.css("color") + "\" backgroundImage=\"" + getImgValue($pagingArea.css("background-image"))
							+ "\"> ";
					var $dhx_pline_skyblue = $(".dhx_pline_skyblue", $pagingArea);
					pagingArea += "<pline width=\"" + $dhx_pline_skyblue.width() + "\" height=\"" + $dhx_pline_skyblue.height() + "\"> ";
					$(".dhx_page_skyblue", $dhx_pline_skyblue).each(
							function(index_page, element_page) {
								var $element_page = $(element_page).children("div").first();
								pagingArea += "<pageNum  backgroundImage=\"" + getImgValue($element_page.css("background-image"))
										+ "\" classes=\"" + $element_page.attr("class") + "\" left=\"" + $element_page.offset().left
										+ "\" top=\"" + $element_page.offset().top + "\" fontFamily=\"" + $element_page.css("font-family")
										+ "\" fontSize=\"" + $element_page.css("font-size") + "\" fontStyle=\""
										+ $element_page.css("font-style") + "\" fontWeight=\"" + $element_page.css("font-weight")
										+ "\" color=\"" + $element_page.css("color") + "\" width=\"" + $element_page.width()
										+ "\" height=\"" + $element_page.height() + "\" value=\"" + getStrValue($element_page.text())
										+ "\" textDecoration=\"" + $element_page.css("text-decoration") + "\" />";
							});
					pagingArea += "</pline>";
					var $pinfo = $(".dhx_pager_info_skyblue", $pagingArea);
					if ($pinfo.length > 0) {
						pagingArea += "<pinfo left=\"" + $pinfo.offset().left + "\" top=\"" + $pinfo.offset().top + "\" fontFamily=\""
								+ $pinfo.css("font-family") + "\" fontSize=\"" + $pinfo.css("font-size") + "\" fontStyle=\""
								+ $pinfo.css("font-style") + "\" fontWeight=\"" + $pinfo.css("font-weight") + "\" color=\""
								+ $pinfo.css("color") + "\" width=\"" + $pinfo.width() + "\" height=\"" + $pinfo.height() + "\" value=\""
								+ getStrValue($pinfo.text()) + "\" textDecoration=\"" + $pinfo.css("text-decoration") + "\" />";
					}
					pagingArea += "</pagingArea>";
					pxml += tablehd + tablebd + pagingArea + "</node>";
				}
			});

	pxml += "</page>";
};

var getTablexml = function(fdiv) {
	var tablehd = "<table width=\"" + fdiv.width() + "\" height=\"" + fdiv.height() + "\" color=\"" + fdiv.css("color")
	// +"\"
	// backgroundImage=\""+getImgValue(fdiv.css("background-image"))
	+ "\" backgroundColor=\"" + fdiv.css("background-color") + "\" classes=\"" + fdiv.attr("class") + "\"> ";
	$("tr", fdiv).each(
			function(index_tr, element_tr) {
				var $element_tr = $(element_tr);
				tablehd += "<tr width=\"" + $element_tr.width() + "\" height=\"" + $element_tr.height() + "\">";
				$("th", $element_tr).each(
						function(index_td, element_td) {
							var $element_td = $(element_td);
							tablehd += "<th width=\"" + $element_td.width() + "\" height=\"" + $element_td.height() + "\" value=\""
									+ getStrValue($element_td.text()) + "\" fontFamily=\"" + $element_td.css("font-family")
									+ "\" fontSize=\"" + $element_td.css("font-size") + "\" fontStyle=\"" + $element_td.css("font-style")
									+ "\" fontWeight=\"" + $element_td.css("font-weight") + "\" color=\"" + $element_td.css("color")
									+ "\" textDecoration=\"" + $element_td.css("text-decoration") + "\" align=\""
									+ $element_td.attr("align") + "\" valign=\"" + $element_td.attr("valign") + "\" paddingleft=\""
									+ $element_td.css("padding-left") + "\"/> ";
						});
				$("td", $element_tr).each(
						function(index_td, element_td) {
							var $element_td = $(element_td);
							tablehd += "<td width=\"" + $element_td.width() + "\" height=\"" + $element_td.height() + "\" value=\""
									+ getStrValue($element_td.text()) + "\" fontFamily=\"" + $element_td.css("font-family")
									+ "\" fontSize=\"" + $element_td.css("font-size") + "\" fontStyle=\"" + $element_td.css("font-style")
									+ "\" fontWeight=\"" + $element_td.css("font-weight") + "\" color=\"" + $element_td.css("color")
									+ "\" textDecoration=\"" + $element_td.css("text-decoration") + "\" align=\""
									+ $element_td.attr("align") + "\" valign=\"" + $element_td.attr("valign") + "\" paddingleft=\""
									+ $element_td.css("padding-left") + "\" /> ";
						});
				tablehd += "</tr>";
			});
	tablehd += "</table>";
	return tablehd;
};

var getStrValue = function(val) {
	var str = $.trim(val);
	// str = str.replace(/&/g,"&amp;");
	str = str.replace(/\<br\>|\<\/br\>/gi, "&#xA;");
	str = str.replace(/\</g, "&lt;");
	str = str.replace(/\>/g, "&gt;");
	str = str.replace(/\'/g, "&apos;");
	str = str.replace(/\"/g, "&quot;");
	return str;
};
var getImgValue = function(val) {
	var str = $.trim(val);
	// str = str.replace(/&/g,"&amp;");
	str = str.replace(/\'/g, "");
	str = str.replace(/\"/g, "");
	return str;
};

var getpxml = function(pDviID) {
	pxml += "</pxml>";
	return pxml;
};
